{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# RAG OpenAI Assistants in AG2\n",
    "\n",
    "This notebook shows an example of the [`GPTAssistantAgent`](https://github.com/ag2ai/ag2/blob/main/autogen/agentchat/contrib/gpt_assistant_agent.py) with retrieval augmented generation. `GPTAssistantAgent` is an experimental AG2 agent class that leverages the [OpenAI Assistant API](https://platform.openai.com/docs/assistants/overview) for conversational capabilities,  working with\n",
    "`UserProxyAgent` in AG2."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import logging\n",
    "import os\n",
    "\n",
    "from autogen import LLMConfig, UserProxyAgent\n",
    "from autogen.agentchat.contrib.gpt_assistant_agent import GPTAssistantAgent\n",
    "\n",
    "logger = logging.getLogger(__name__)\n",
    "logger.setLevel(logging.WARNING)\n",
    "\n",
    "assistant_id = os.environ.get(\"ASSISTANT_ID\", None)\n",
    "\n",
    "llm_config = LLMConfig.from_json(path=\"OAI_CONFIG_LIST\")\n",
    "assistant_config = {\n",
    "    \"assistant_id\": assistant_id,\n",
    "    \"tools\": [{\"type\": \"retrieval\"}],\n",
    "    \"file_ids\": [\"file-AcnBk5PCwAjJMCVO0zLSbzKP\"],\n",
    "    # add id of an existing file in your openai account\n",
    "    # in this case I added the implementation of conversable_agent.py\n",
    "}\n",
    "\n",
    "gpt_assistant = GPTAssistantAgent(\n",
    "    name=\"assistant\",\n",
    "    instructions=\"You are adapt at question answering\",\n",
    "    llm_config=llm_config,\n",
    "    assistant_config=assistant_config,\n",
    ")\n",
    "\n",
    "user_proxy = UserProxyAgent(\n",
    "    name=\"user_proxy\",\n",
    "    code_execution_config=False,\n",
    "    is_termination_msg=lambda msg: \"TERMINATE\" in msg[\"content\"],\n",
    "    human_input_mode=\"ALWAYS\",\n",
    ")\n",
    "user_proxy.initiate_chat(gpt_assistant, message=\"Please explain the code in this file!\")\n",
    "\n",
    "gpt_assistant.delete_assistant()"
   ]
  }
 ],
 "metadata": {
  "front_matter": {
   "description": "OpenAI Assistant with retrieval augmentation.",
   "tags": [
    "RAG",
    "OpenAI Assistant"
   ]
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
